/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Generated By:JJTree: Do not edit this line. AstBracketSuffix.java */

package org.jboss.el.parser;

import javax.el.ELException;
import javax.el.ELResolver;
import javax.el.MethodInfo;
import javax.el.PropertyNotFoundException;

import org.jboss.el.lang.EvaluationContext;
import org.jboss.el.util.MessageFactory;
import org.jboss.el.util.ReflectionUtil;




/**
 * @author Jacob Hookom [jacob@hookom.net]
 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
 */
@SuppressWarnings("rawtypes")
public final class AstBracketSuffix extends ValueSuffixNode {
	public AstBracketSuffix(int id) {
		super(id);
	}

	public Object getValue(EvaluationContext ctx)
			throws ELException {
		return this.children[0].getValue(ctx);
	}

	public MethodInfo getMethodInfo(Object base, EvaluationContext ctx, Class[] paramTypes) throws ELException {
		return ReflectionUtil.getMethodInfo(base, this.getValue(ctx), paramTypes);
	}

	public Class getType(Object base, EvaluationContext ctx) throws ELException {
		ELResolver resolver = ctx.getELResolver();
		ctx.setPropertyResolved(false);
		return resolver.getType(ctx, base, this.getValue(ctx));
	}
	
	public Object getTarget(Object base, EvaluationContext ctx) throws ELException {
		if (base == null) return null;
		
		ELResolver resolver = ctx.getELResolver();
		ctx.setPropertyResolved(false);
		Object p = this.getValue(ctx);
		Object r = resolver.getValue(ctx, base, p);
		if (r == null) {
			throw new PropertyNotFoundException(MessageFactory.get(
					"error.unreachable.property", base.getClass().getName(),
					p));
		}
		return r;
	}

	public Object getValue(Object base, EvaluationContext ctx) throws ELException {
		if (base == null) return null;
		
		ELResolver resolver = ctx.getELResolver();
		ctx.setPropertyResolved(false);
		return resolver.getValue(ctx, base, this.getValue(ctx));
	}

	public Object invoke(Object base, EvaluationContext ctx, Class[] paramTypes, Object[] paramValues) throws ELException {
		return ReflectionUtil.invokeMethod(base, this.getValue(ctx), paramTypes, paramValues);
	}

	public boolean isReadOnly(Object base, EvaluationContext ctx) throws ELException {
		ELResolver resolver = ctx.getELResolver();
		ctx.setPropertyResolved(false);
		return resolver.isReadOnly(ctx, base, this.getValue(ctx));
	}

	public void setValue(Object base, EvaluationContext ctx, Object value) throws ELException {
		ELResolver resolver = ctx.getELResolver();
		ctx.setPropertyResolved(false);
		resolver.setValue(ctx, base, this.getValue(ctx), value);
	}
}
